/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * EditarLogin.java
 *
 * Created on 05/04/2011, 00:49:17
 */
package editar;

/**
 *
 * @author gladson
 */
import Classe.BD;
import Classe.Login;
import Classe.Validacao;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFrame;
import javax.swing.JOptionPane;

public class EditarLogin extends Validacao {

    /** Creates new form EditarLogin */
    public EditarLogin() {
        initComponents();
        this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);

    }
    int idFun;
    int semLogin;

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        pnlCadastroLogin = new javax.swing.JPanel();
        btSalvarCadastroLogin = new javax.swing.JButton();
        btSairCadastroLogin = new javax.swing.JButton();
        sTxtComfirmaCadastroLogin = new javax.swing.JPasswordField();
        lblCorfirmaCadastroLogin = new javax.swing.JLabel();
        sTxtSenhaCadastroLogin = new javax.swing.JPasswordField();
        lblSenhaCadastroLogin = new javax.swing.JLabel();
        sLoginCadastroLogin = new javax.swing.JTextField();
        lblLoginCadastroLogin = new javax.swing.JLabel();
        sTxtFuncionarioCadastroLogin = new javax.swing.JTextField();
        lblFuncionarioCadastroLogin = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setResizable(false);

        jPanel1.setBackground(new java.awt.Color(255, 255, 255));

        pnlCadastroLogin.setBackground(new java.awt.Color(255, 255, 255));
        pnlCadastroLogin.setBorder(javax.swing.BorderFactory.createTitledBorder(null, " Editar Cadastro Login ", javax.swing.border.TitledBorder.CENTER, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("PMingLiU", 0, 36))); // NOI18N

        btSalvarCadastroLogin.setFont(new java.awt.Font("Arial", 0, 12));
        btSalvarCadastroLogin.setText("Salvar");
        btSalvarCadastroLogin.setToolTipText("Salvar");
        btSalvarCadastroLogin.setBorder(javax.swing.BorderFactory.createCompoundBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED), javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED)));
        btSalvarCadastroLogin.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btSalvarCadastroLoginActionPerformed(evt);
            }
        });

        btSairCadastroLogin.setFont(new java.awt.Font("Arial", 0, 12)); // NOI18N
        btSairCadastroLogin.setText("Sair");
        btSairCadastroLogin.setToolTipText("Sair");
        btSairCadastroLogin.setBorder(javax.swing.BorderFactory.createCompoundBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED), javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED)));
        btSairCadastroLogin.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btSairCadastroLoginActionPerformed(evt);
            }
        });

        sTxtComfirmaCadastroLogin.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));

        lblCorfirmaCadastroLogin.setFont(new java.awt.Font("Arial", 1, 11));
        lblCorfirmaCadastroLogin.setText("Confirma");

        sTxtSenhaCadastroLogin.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));

        lblSenhaCadastroLogin.setFont(new java.awt.Font("Arial", 1, 11));
        lblSenhaCadastroLogin.setText("Senha");

        sLoginCadastroLogin.setFont(new java.awt.Font("Arial", 0, 12));
        sLoginCadastroLogin.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));

        lblLoginCadastroLogin.setFont(new java.awt.Font("Arial", 1, 11));
        lblLoginCadastroLogin.setText("Login");

        sTxtFuncionarioCadastroLogin.setBorder(javax.swing.BorderFactory.createEtchedBorder(javax.swing.border.EtchedBorder.RAISED));

        lblFuncionarioCadastroLogin.setFont(new java.awt.Font("Arial", 1, 11));
        lblFuncionarioCadastroLogin.setText("Funcionário");

        javax.swing.GroupLayout pnlCadastroLoginLayout = new javax.swing.GroupLayout(pnlCadastroLogin);
        pnlCadastroLogin.setLayout(pnlCadastroLoginLayout);
        pnlCadastroLoginLayout.setHorizontalGroup(
            pnlCadastroLoginLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlCadastroLoginLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(pnlCadastroLoginLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(pnlCadastroLoginLayout.createSequentialGroup()
                        .addComponent(btSalvarCadastroLogin)
                        .addGap(18, 18, 18)
                        .addComponent(btSairCadastroLogin)
                        .addGap(314, 314, 314))
                    .addGroup(pnlCadastroLoginLayout.createSequentialGroup()
                        .addGroup(pnlCadastroLoginLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(sTxtComfirmaCadastroLogin, javax.swing.GroupLayout.DEFAULT_SIZE, 456, Short.MAX_VALUE)
                            .addComponent(lblCorfirmaCadastroLogin)
                            .addComponent(lblSenhaCadastroLogin)
                            .addComponent(lblLoginCadastroLogin)
                            .addComponent(lblFuncionarioCadastroLogin)
                            .addComponent(sTxtSenhaCadastroLogin, javax.swing.GroupLayout.DEFAULT_SIZE, 456, Short.MAX_VALUE)
                            .addComponent(sLoginCadastroLogin, javax.swing.GroupLayout.DEFAULT_SIZE, 456, Short.MAX_VALUE)
                            .addComponent(sTxtFuncionarioCadastroLogin, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 456, Short.MAX_VALUE))
                        .addContainerGap())))
        );

        pnlCadastroLoginLayout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {btSairCadastroLogin, btSalvarCadastroLogin});

        pnlCadastroLoginLayout.setVerticalGroup(
            pnlCadastroLoginLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, pnlCadastroLoginLayout.createSequentialGroup()
                .addComponent(lblFuncionarioCadastroLogin)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(sTxtFuncionarioCadastroLogin, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(lblLoginCadastroLogin)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(sLoginCadastroLogin, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(lblSenhaCadastroLogin)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 12, Short.MAX_VALUE)
                .addComponent(sTxtSenhaCadastroLogin, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(lblCorfirmaCadastroLogin)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(sTxtComfirmaCadastroLogin, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(29, 29, 29)
                .addGroup(pnlCadastroLoginLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btSalvarCadastroLogin)
                    .addComponent(btSairCadastroLogin))
                .addContainerGap())
        );

        pnlCadastroLoginLayout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] {btSairCadastroLogin, btSalvarCadastroLogin});

        pnlCadastroLoginLayout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] {sLoginCadastroLogin, sTxtComfirmaCadastroLogin, sTxtFuncionarioCadastroLogin, sTxtSenhaCadastroLogin});

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(pnlCadastroLogin, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(pnlCadastroLogin, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-516)/2, (screenSize.height-347)/2, 516, 347);
    }// </editor-fold>//GEN-END:initComponents

    private void btSairCadastroLoginActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btSairCadastroLoginActionPerformed
        this.dispose();
        // TODO add your handling code here:
}//GEN-LAST:event_btSairCadastroLoginActionPerformed

    private void btSalvarCadastroLoginActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btSalvarCadastroLoginActionPerformed
        if (validar()) {

            if (semLogin == 1) {

                Login login = new Login();
                login.setLogin(sLoginCadastroLogin.getText().trim().toUpperCase());
                login.setSenha(new String(sTxtSenhaCadastroLogin.getPassword()));
                login.setConfSenha(new String(sTxtComfirmaCadastroLogin.getPassword()));

                if (login.getSenha().equalsIgnoreCase(login.getConfSenha())) {
                    try {
                        String sql = "INSERT INTO LOGIN(ID_LOGIN,LOGIN,SENHA,ID_FUNCIONARIO)VALUES(SQ_LOGIN.NEXTVAL,?,?,?)";
                        Object[] parametros = {login.getLogin(), login.getSenha().trim().toUpperCase(), idFun};
                        Object[] tipos = {String.class, String.class, Integer.class};
                        BD banco = new BD();

                        banco.executar(sql, parametros, tipos);
                        JOptionPane.showMessageDialog(rootPane, "Atualização realizada com Sucesso");
                        this.dispose();
                    } catch (Exception ex) {
                        Logger.getLogger(EditarLogin.class.getName()).log(Level.SEVERE, null, ex);
                    }
                } else {
                    JOptionPane.showMessageDialog(rootPane, "Confirmação de senha não confere!!");
                }


            } else {

                Login login = new Login();
                login.setLogin(sLoginCadastroLogin.getText().trim().toUpperCase());
                login.setSenha(new String(sTxtSenhaCadastroLogin.getPassword()));
                login.setConfSenha(new String(sTxtComfirmaCadastroLogin.getPassword()));

                if (login.getSenha().equalsIgnoreCase(login.getConfSenha())) {
                    try {
                        String sql = "UPDATE LOGIN SET LOGIN=?,SENHA=?,ID_FUNCIONARIO=? WHERE ID_FUNCIONARIO=" + idFun;
                        Object[] parametros = {login.getLogin(), login.getSenha().trim().toUpperCase(), idFun};
                        Object[] tipos = {String.class, String.class, Integer.class};
                        BD banco = new BD();


                        banco.executar(sql, parametros, tipos);
                        JOptionPane.showMessageDialog(rootPane, "Atualização realizada com Sucesso");
                        this.dispose();
                    } catch (Exception ex) {
                        Logger.getLogger(EditarLogin.class.getName()).log(Level.SEVERE, null, ex);
                    }
                } else {
                    JOptionPane.showMessageDialog(rootPane, "Confirmação de senha não confere!!");
                }
            }
        }
    }//GEN-LAST:event_btSalvarCadastroLoginActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                //      new EditarLogin().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btSairCadastroLogin;
    private javax.swing.JButton btSalvarCadastroLogin;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JLabel lblCorfirmaCadastroLogin;
    private javax.swing.JLabel lblFuncionarioCadastroLogin;
    private javax.swing.JLabel lblLoginCadastroLogin;
    private javax.swing.JLabel lblSenhaCadastroLogin;
    private javax.swing.JPanel pnlCadastroLogin;
    private javax.swing.JTextField sLoginCadastroLogin;
    private javax.swing.JPasswordField sTxtComfirmaCadastroLogin;
    private javax.swing.JTextField sTxtFuncionarioCadastroLogin;
    private javax.swing.JPasswordField sTxtSenhaCadastroLogin;
    // End of variables declaration//GEN-END:variables

    public void iniciar(int idFunc) {

        idFun = idFunc;

        try {
            String sql = "SELECT * FROM LOGIN WHERE ID_FUNCIONARIO=" + idFun;
            BD bd = new BD();
            ResultSet r1 = bd.consultar(sql);

            if (r1.next()) {
                String sql2 = "SELECT F.NOME_FUNCIONARIO, L.LOGIN FROM LOGIN L INNER JOIN FUNCIONARIO F ON F.ID_FUNCIONARIO = L.ID_FUNCIONARIO WHERE F.ID_FUNCIONARIO=" + idFun;
                ResultSet r2 = bd.consultar(sql2);
                if (r2.next()) {
                    sTxtFuncionarioCadastroLogin.setText(r2.getString("NOME_FUNCIONARIO"));
                    sTxtFuncionarioCadastroLogin.setEnabled(false);
                    sLoginCadastroLogin.setText(r2.getString("LOGIN"));
                }
            } else {
                semLogin = 1;
                String sql3 = "SELECT * FROM FUNCIONARIO WHERE ID_FUNCIONARIO=" + idFun;
                ResultSet r3 = bd.consultar(sql3);
                if (r3.next()) {
                    sTxtFuncionarioCadastroLogin.setText(r3.getString("NOME_FUNCIONARIO"));
                    sTxtFuncionarioCadastroLogin.setEnabled(false);

                }
            }
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(EditarLogin.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(EditarLogin.class.getName()).log(Level.SEVERE, null, ex);
        }



    }

    public boolean validar() {
        boolean Resultado = true;

        String senha = (new String(sTxtSenhaCadastroLogin.getPassword()));
        String confsenha = (new String(sTxtComfirmaCadastroLogin.getPassword()));

        if (validarLogin(senha, confsenha) == false) {
            Resultado = false;
        }

        return Resultado;
    }
}
